class Admin::CampaignsController < AdminController
  
  
  def show
    @campaign = Campaign.find(params[:id])
  end
  
  def edit
    @campaign = Campaign.find(params[:id])
  end
  
  def update
    @campaign = Campaign.find(params[:id])
    respond_to do |format|
      if @campaign.update_attributes(params[:campaign])
        flash_notice(:campaign_updated)
        format.html { redirect_to admin_campaign_url(@campaign) }
        format.xml  { head :ok }
      else
        flash_error(:campaign_not_updated)
        format.html { render :action => "edit" }
        format.xml  { render :xml => @campaign.errors, :status => :unprocessable_entity }
      end
    end
    
  end
  
  
  def ended
    #@campaigns = Campaign.ended_campaigns
        
        
    @campaigns = Campaign.ended_campaigns.paginate(:order=>"points DESC",:page => params[:page], :per_page=>WebappConstants::RECORDS_PER_PAGE)
    #@campaigns = Campaign.ended_campaigns.paginate(:order=>"points DESC",:page => params[:page], :per_page=>1)
        
  end
  
  def new
    @campaign = Campaign.new
  end
  def index
    @campaigns = Campaign.all
  end
  
  def create
    @campaign = Campaign.new(params[:campaign])
    @campaign.owner_id = @current_user.id
    if @campaign.save
      flash_notice(:campaign_created)
      
      redirect_to admin_campaigns_url
    else 
      render :new
    end   
  end
  
  def destroy
    @campaign = Campaign.find(params[:id])
    @campaign.destroy
    flash_notice(:campaign_removed)
    respond_to do |format|
      format.html { redirect_to(admin_campaigns_url) }
      format.xml  { head :ok }
    end
  end
  
  def copy
    original_campaign = Campaign.find(params[:id])
    @campaign = original_campaign.clone
    @campaign.owner_id = @current_user.id
    @campaign.is_default=false
    render :new
  end
end
